<?php


namespace app\app\controller;


class Review extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getInfo()
    {
        $app_id = input('app_id');
        $device_id = input('device_id');
        if (empty($app_id) || empty($device_id)) {
            $this->error('数据有误！',null,['menu_show'=>0]);
        }

        $deviceInfo = db('device')->where('app_id', $app_id)->where('device_id', $device_id)->find();
//        dump($deviceInfo);
        if (empty($deviceInfo)) {
            $this->error('数据获取失败',null,['menu_show'=>0]);
        }
        if ($deviceInfo['review_status'] > 0) {
            $this->success('该用户已经参加过活动了',['menu_show'=>0]);
        }
        $reviewDeviceInfo = db('review_device')->where('app_id', $app_id)->where('device_id', $device_id)->order('id', 'desc')->find();
        if (empty($reviewDeviceInfo)) {
            $this->error('用户不再活动列表中',null,['menu_show'=>0]);
        }

        if ($reviewDeviceInfo['is_show'] == 1) {
            $this->success('用户在活动列表中', [
                'review_id' => $reviewDeviceInfo['review_id'],
                'id' => $reviewDeviceInfo['id'],
                'pic' => $reviewDeviceInfo['pic'],
                'menu_show'=>1
            ]);
        } else {
            $this->error('该活动已经展示过了！',null,[
                'review_id' => $reviewDeviceInfo['review_id'],
                'id' => $reviewDeviceInfo['id'],
                'pic' => $reviewDeviceInfo['pic'],
                'menu_show'=>1,
            ]);
        }

    }

    /***
     * 保存好评图片
     */
    public function sendpic()
    {
        $input = input();
        $pic = $input['pic'];
        $device_id = $input['device_id'];
        $app_id = $input['app_id'];
        if (empty($device_id) || empty($app_id)) {
            $this->error('数据有误');
        }
        if (empty($pic)) {
            $this->error('请先上传图片');
        }
        $reviewDeviceInfo = db('review_device')->where('app_id', $app_id)->where('device_id', $device_id)->order('id', 'desc')->find();
        if (empty($reviewDeviceInfo)) {
            $this->error('未能找到相应信息');
        }
        db('review_device')->where('id', $reviewDeviceInfo['id'])->update([
            'pic' => $pic,
            'is_show'=>0,
            'time_updated'=>time()
        ]);
        $this->updateReview($reviewDeviceInfo['review_id']);
        $this->success('提交成功，请等待管理员审核！',[
            'review_id' => $reviewDeviceInfo['review_id'],
            'id' => $reviewDeviceInfo['id'],
            'pic' => $pic
        ]);
    }

    /**
     * 好评活动弹窗确认
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     *
     */
    public function confirmShow(){
        $input = input();
        $device_id = $input['device_id'];
        $app_id = $input['app_id'];
        if (empty($device_id) || empty($app_id)) {
            $this->error('数据有误');
        }
        $reviewDeviceInfo = db('review_device')->where('app_id', $app_id)->where('device_id', $device_id)->order('id', 'desc')->find();
        if (empty($reviewDeviceInfo)) {
            $this->error('未能找到相应信息');
        }
        db('review_device')->where('app_id', $app_id)->where('device_id', $device_id)->update([
            'is_show'=>0,
            'time_updated'=>time()
        ]);
        $this->updateReview($reviewDeviceInfo['review_id']);
        $this->success('操作已确认！');
    }
    public function updateReview($reviewId)
    {
        if ($reviewId < 1) {
            $this->error('数据有误！');
        }
        $reviewInfo = db('review')->where('id', $reviewId)->find();
        if (empty($reviewInfo)) {
            $this->error('数据有误！');
        }
        //更新设备数量
        $count = db('review_device')->where('review_id', $reviewId)->count() ?: 0;
        $showCount = db('review_device')->where('review_id', $reviewId)->where('is_show', 0)->count() ?: 0;
        $uploadCount = db('review_device')->where('review_id', $reviewId)->whereNotNull('pic')->count() ?: 0;
        $doCount = db('review_device')->where('review_id', $reviewId)->where('is_rewarded', 1)->count() ?: 0;
        db('review')->where('id', $reviewId)->update([
            'device_num' => $count,
            'show_num' => $showCount,
            'upload_num' => $uploadCount,
            'do_num' => $doCount,
        ]);
    }
}